import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  roleList: [],
  currentRole: null,
  userMenus: [],
  allMenus: [],
  admins: [],
  currentAdmin: {},
  currentMenu: {},
  userList: {}
};

const getters = {
  roleList: state => state.roleList,
  userMenus: state => state.userMenus,
  allMenus: state => state.allMenus,
  currentRole: state => state.currentRole,
  admins: state => state.admins,
  currentAdmin: state => state.currentAdmin,
  userList: state => state.userList,
  currentMenu: state => state.currentMenu
};

const actions = {
  getRoles({ commit }) {
    return Api.getRoles({}).then(ret => {
      // console.log("roles", JSON.stringify(ret.data.data));
      commit(types.UPDATE_ROLE_LIST, ret.data.data);
      return ret;
    });
  },
  getUserMenus({ commit }) {
    return Api.getAuthUserMenus({ menuSys: "Admin" }).then(ret => {
      // console.log("user menus:", ret.data.data);
      commit(types.UPDATE_USER_MENUS, ret.data.data);
      return ret;
    });
  },
  getAllMenus({ commit }) {
    return Api.getMenuTree({ menuSys: "Admin" }).then(ret => {
      commit(types.UPDATE_ALL_MENUS, ret.data.data);
      return ret;
    });
  },
  getMenuIDTree({ commit }) {
    return Api.getMenuIDTree({ menuSys: "Admin" }).then(ret => {
      console.log("all menu", JSON.stringify(ret.data.data));
    });
  },
  addMenu({ commit }, payload) {
    return Api.addMenu({ body: payload }).then(ret => {
      // console.log("add menu", ret);
      return ret;
    });
  },
  getOneMenuS({ commit }, payload) {
    return Api.getOneMenu(payload).then(ret => {
      commit(types.UPDATE_CURRENT_MENUS, ret.data.data);
      return ret;
    });
  },
  updateCurrentRole({ commit }, payload) {
    commit(types.UPDATE_CURRENT_ROLE, payload);
  },
  initCurrentRole({ commit }, payload) {
    commit(types.INIT_CURRENT_ROLE, payload);
  },
  createRole({ commit }) {
    commit(types.CREATE_ROLE);
  },
  saveRole({ commit }, payload) {
    return Api.addOrUpdateRole({ body: payload }).then(ret => {
      return ret;
    });
  },
  delRole({ commit }, param) {
    return Api.deleteRole(param).then(ret => {
      return ret;
    });
  },
  getAuthUsersS({ commit }, param) {
    return Api.getAuthUsers(param).then(ret => {
      commit(types.UPDATE_ADMINS, ret.data.data);
      return ret;
    });
  },
  getAdminUserPageS({ commit }, param) {
    return Api.getAdminUserPage(param).then(ret => {
      commit(types.UPDATE_ADMINS, ret.data.data);
      return ret;
    });
  },
  getTSUserPageS({ commit }, param) {
    return Api.getTSUserPage(param).then(ret => {
      commit(types.UPDATE_TUSER_LIST, ret.data.data);
      return ret;
    });
  },
  addAuthUsers({ commit }, payload) {
    return Api.addAuthUser(payload).then(ret => {
      return ret;
    });
  },
  addAdminUserS({ commit }, payload) {
    return Api.addAdminUser(payload).then(ret => {
      return ret;
    });
  },
  addTSUserS({ commit }, payload) {
    return Api.updateUserInfo({ body: payload }).then(ret => {
      return ret;
    });
  },
  checkUserS({ commit }, payload) {
    return Api.checkUser(payload).then(ret => {
      return ret;
    });
  },
  updateAuthUserStateS({ commit }, payload) {
    return Api.updateAuthUserState(payload).then(ret => {
      return ret;
    });
  },
  updateAdmin({ commit }, payload) {
    return Api.updateAuthUser(payload).then(ret => {
      return ret;
    });
  },
  updateAdminUserS({ commit }, payload) {
    return Api.updateAdminUser(payload).then(ret => {
      return ret;
    });
  },
  resetAdminPassowrd({ commit }, payload) {
    return Api.resetAuthUserPassword(payload).then(ret => {
      return ret;
    });
  },
  addUserRole({ commit }, param) {
    return Api.addUserRole(param).then(ret => {
      return ret;
    });
  },
  deleteUserRole({ commit }, param) {
    return Api.deleteAuthUserRole(param).then(ret => {
      return ret;
    });
  },
  getTSUserByUserName({ commit }, param) {
    return Api.getTSUserByUserName(param).then(ret => {
      commit(types.UPDATE_CURRENT_ADMIN, ret.data.data);
      return ret;
    });
  },
  getOneUserInfoS({ commit }, param) {
    return Api.getOneUserInfo(param).then(ret => {
      commit(types.UPDATE_CURRENT_ADMIN, ret.data.data);
      return ret;
    });
  },
  getAdminUserS({ commit }, param) {
    return Api.getAdminUser(param).then(ret => {
      commit(types.UPDATE_CURRENT_ADMIN, ret.data.data);
      return ret;
    });
  },
  deleteAdminUserS({ commit }, param) {
    return Api.deleteAdminUser(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.UPDATE_ROLE_LIST](state, payload) {
    state.roleList = payload;
  },
  [types.UPDATE_USER_MENUS](state, payload) {
    state.userMenus = payload;
  },
  [types.UPDATE_ALL_MENUS](state, payload) {
    state.allMenus = payload;
  },
  [types.INIT_CURRENT_ROLE](state) {
    state.currentRole = {
      roleName: "",
      remarks: "",
      menusIds: []
    };
  },
  [types.CREATE_ROLE](state) {
    const role = {
      roleName: "新职位",
      remarks: "",
      menusIds: [],
      status: "unsave",
      _tmp_key: Math.random()
        .toString(36)
        .substring(3, 8)
    };
    state.roleList.push(role);
  },
  [types.RESET_CURRENT_ROLE](state) {
    state.currentRole = null;
  },
  [types.INIT_CURRENT_ADMIN](state) {
    state.currentAdmin = {
      name: "",
      mobile: "",
      username: "",
      pwd: "",
      password: "",
      userRole: "ADMIN",
      depart: { id: "" },
      usData: "ALL",
      duty: "",
      userType: "",
      proNum: "",
      proType: "",
      sex: "",
      birth: "",
      education: "",
      profession: "",
      area: ""
    };
  },
  [types.INIT_CURRENT_DEPART_ADMIN](state, para) {
    state.currentAdmin = {
      name: "",
      mobile: "",
      username: "",
      pwd: "",
      userRole: "ADMIN",
      depart: { id: para.departId },
      usData: "ALL",
      duty: "",
      userType: "",
      proNum: "",
      proType: "",
      sex: "",
      birth: "",
      education: "",
      profession: "",
      area: ""
    };
  },
  [types.INIT_CURRENT_MENU](state) {
    state.currentMenu = {
      menuName: "",
      parentMenuId: null,
      icon: "",
      menuType: "Menu",
      menuSys: "Admin",
      sort: 0,
      route: "",
      permissions: []
    };
  },
  [types.INIT_CURRENT_EXPERT](state) {
    state.currentAdmin = {
      name: "",
      mobile: "",
      username: "",
      pwd: "",
      userRole: "USER",
      depart: { id: "" },
      usData: "ALL",
      userType: "zks",
      sex: "",
      idCard: "",
      birth: ""
    };
  },
  [types.UPDATE_CURRENT_ADMIN](state, payload) {
    state.currentAdmin = payload;
  },
  [types.UPDATE_CURRENT_MENUS](state, payload) {
    state.currentMenu = payload;
  },
  [types.UPDATE_CURRENT_ROLE](state, payload) {
    state.currentRole = payload;
  },
  [types.UPDATE_ADMINS](state, payload) {
    state.admins = payload;
  },
  [types.UPDATE_TUSER_LIST](state, payload) {
    state.userList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
